package com.fastdfs.proto.storage;


import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfReader;
import css.sword.util.Doc2Docx;
import css.sword.util.PdfUtils;
import fr.opensagres.xdocreport.utils.StringUtils;
import org.apache.commons.io.IOUtils;

import java.io.*;


public class ConvertTxt2PdfStream implements DownloadCallback<Integer>
{
	private OutputStream out;
	private long currentBytes = 0;
	private String userPassword;
	private String ownerPassword;
	private String waterMark;
	private byte[] redFile;

	public OutputStream getOut() {
		return out;
	}

	public void setOut(OutputStream out) {
		this.out = out;
	}

	public String getUserPassword() {
		return userPassword;
	}

	public void setUserPassword(String userPassword) {
		this.userPassword = userPassword;
	}

	public String getOwnerPassword() {
		return ownerPassword;
	}

	public void setOwnerPassword(String ownerPassword) {
		this.ownerPassword = ownerPassword;
	}

	public ConvertTxt2PdfStream(OutputStream out, String waterMark)
	{
		super();
		this.out = out;
		this.waterMark=waterMark;
	}

	@Override
	public Integer recv(InputStream ins)  {
		try {
			    byte[] abyte=IOUtils.toByteArray(ins);
				if (StringUtils.isEmpty(waterMark)) {
					PdfUtils.text2pdf(new ByteArrayInputStream(abyte),out);
				}else{
					ByteArrayOutputStream os = new ByteArrayOutputStream();
					PdfUtils.text2pdf(new ByteArrayInputStream(abyte),os);
					ByteArrayInputStream swapStream = new ByteArrayInputStream(os.toByteArray());
					PdfUtils.waterMarkerText(new PdfReader(swapStream),out,waterMark);
				}

//				// 通过ioutil 对接输入输出流，实现文件下载
//				PdfOptions options = PdfOptions.create();
//				if (StringUtils.isNotEmpty(userPassword) || StringUtils.isNotEmpty(ownerPassword)) {
//					options.setConfiguration(new IPdfWriterConfiguration() {
//						public void configure(PdfWriter writer) throws DocumentException {
//							writer.setEncryption(userPassword.getBytes(), ownerPassword.getBytes(), PdfWriter.ALLOW_COPY, PdfWriter.ENCRYPTION_AES_128);
//						}
//					});
//				}
//				byte[] abyte=IOUtils.toByteArray(ins);
//				XWPFDocument document =
//						new XWPFDocument(new ByteArrayInputStream(abyte));
//				if (StringUtils.isEmpty(waterMark)) {
//						try {
//							PdfConverter.getInstance().convert(document, out, options);
//						} catch (Exception e) {
//							e.printStackTrace();
//							document.close();
//							Doc2Docx.convertDocToPdf(new ByteArrayInputStream(abyte), out);
//						}
//
//				} else {
//					ByteArrayOutputStream os = new ByteArrayOutputStream();
//					PdfConverter.getInstance().convert(document, os, options);
//					ByteArrayInputStream swapStream = new ByteArrayInputStream(os.toByteArray());
//					PdfUtils.waterMarkerText(new PdfReader(swapStream), out, waterMark);
//				}
//			}
		//	IOUtils.copy(in, out);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		} finally {
			// 关闭流
			IOUtils.closeQuietly(ins);
//			IOUtils.closeQuietly(out);
		}
		return 0;
	}
}
